<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 钉钉机器人ActionCard消息 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace ding\msg\robot;

use common\Markdown;

/**
 * 给机器人发送的ActionCard消息
 *
 * @package ding\msg\robot
 */
class ActionCardRobotMsg extends RobotMsg {
    protected $title;
    protected $markdownText;
    /**
     * 按钮的信息：title-按钮标题，actionURL-点击按钮触发的URL
     * @var
     */
    protected $btns;
    /**
     * 0-正常发消息者头像,1-隐藏发消息者头像
     * @var
     */
    protected $hideAvatar;
    /**
     * 0-按钮竖直排列，1-按钮横向排列
     * @var
     */
    protected $btnOrientation;
    /**
     * 当$btns只有一项时，是否采用singleBtnStyle样式
     * @var
     */
    protected $singleBtnStyle;

    /**
     * ActionCard消息构造函数（单按钮）
     *
     * @param $title
     * @param $markdownText
     * @param $btns            array('按钮标题名称'=>'按钮url','按钮2标题名称'=>'按钮url')
     * @param $hideAvatar
     * @param $btnOrientation
     * @param $singleBtnStyle
     */
    public function __construct($title, $markdownText, $btns, $hideAvatar = 0, $btnOrientation = 0, $singleBtnStyle = false) {
        parent::__construct("actionCard");
        $this->title = $title;
        $this->markdownText = $markdownText;
        $this->btns = $btns;
        $this->hideAvatar = "" . $hideAvatar;
        $this->btnOrientation = "" . $btnOrientation;
        $this->singleBtnStyle = $singleBtnStyle;
    }

    /**
     * 生成actionCard主体的markdown文本
     *
     * @param $bannerUrl
     * @param $briefTitle
     * @param $briefText
     * @return string
     */
    public static function getActionCardBodyText($bannerUrl, $briefTitle, $briefText) {
        return Markdown::getImageText($bannerUrl) . "\n" .
            Markdown::getHeaderText(3, $briefTitle) . "\n" .
            $briefText;
    }

    /**
     * 转换为可发送的消息
     * {
     *     "actionCard": {
     *         "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
     *         "text": "![screenshot](@lADOpwk3K80C0M0FoA)"+
     *                 "### 乔布斯 20 年前想打造的苹果咖啡厅"+
     *                 "Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
     *         "hideAvatar": "0",
     *         "btnOrientation": "0",
     *         "btns": [
     *             {
     *                 "title": "内容不错",
     *                 "actionURL": "https://www.dingtalk.com/"
     *             },
     *             {
     *                 "title": "不感兴趣",
     *                 "actionURL": "https://www.dingtalk.com/"
     *             }
     *         ]
     *     },
     *     "msgtype": "actionCard"
     * }
     * @return array
     */
    public function toMsg() {
        $actionCard = [
            'title' => $this->title,
            'text' => $this->markdownText,
            'hideAvatar' => $this->hideAvatar,
            'btnOrientation' => $this->btnOrientation
        ];
        if (count($this->btns) == 1 && $this->singleBtnStyle) {
            foreach ($this->btns as $title => $url) {
                $actionCard['singleTitle'] = $title;
                $actionCard['singleURL'] = $url;
            }
        } else {
            $actionCard['btns'] = [];
            foreach ($this->btns as $title => $url) {
                $actionCard['btns'][] = [
                    "title" => $title,
                    "actionURL" => $url
                ];
            }
        }
        return [
            'msgtype' => $this->msgtype,
            'actionCard' => $actionCard
        ];
    }

}
